<template>
  <div>
    <!--    搜索表单-->
    <div style="margin-bottom: 20px">
      <el-input style="width: 240px" placeholder="请输入用户名" v-model="params.username"></el-input>
      <el-input style="width: 240px; margin-left: 5px" placeholder="请输入联系方式" v-model="params.phone"></el-input>
      <el-input style="width: 240px; margin-left: 5px" placeholder="请输入邮箱" v-model="params.email"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="initAdmin"><i class="el-icon-search"></i> 搜索
      </el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh-right"></i> 重置
      </el-button>
    </div>

    <el-table :data="tableData" stripe>
      <el-table-column prop="id" label="编号"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="phone" label="联系方式"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="scope">
          <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              @change="statusChange(scope.row)"
              inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <!--          scope.row 就是当前行数据-->
          <!--        <el-button type="warning" @click="handleAccountAdd(scope.row)">充值</el-button>-->
          <el-button type="primary" @click="$router.push('/editAdmin?id=' + scope.row.id)">编辑</el-button>
          <el-popconfirm
              style="margin-left: 5px"
              title="您确定删除这行数据吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除</el-button>
          </el-popconfirm>
          <el-button type="warning" style="margin-left: 5px" @click="handleChangePass(scope.row)" >修改密码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改密码" :visible="dialogFormVisible" width="40%" >
      <el-form :model="form" style="width: 600px" :rules="rules" ref="dialogForm">
        <el-form-item label="新密码"  prop="newPass">
          <el-input v-model="form.newPass" show-password></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveChange">确 定</el-button>
      </div>
    </el-dialog>


    <!--    分页-->
    <div style="margin-top: 20px">
      <el-pagination
          background
          :curren-page="params.pageNum"
          :page-size="params.pageSize"
          layout="total, prev, pager, next"
          @current-change="handleCurrentChange"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";
export default {
  name: 'Admin',
  components: {},
  data() {
    return {
      rules:{
        newPass: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在3-10个字符', trigger: 'blur'}
        ]
      },
      admin:Cookies.get('admin')?JSON.parse(Cookies.get('admin')):{},
      form:{},
      dialogFormVisible:false,
      tableData: [],
      total: 0,
      params: {
        pageSize: 10,
        pageNum: 1,
        username: '',
        phone: '',
        email: ''
      }
    }
  },
  created() {
    this.initAdmin()
  },
  methods: {
    statusChange(row){
      //当前用户不能修改自己的状态
      if(this.admin.id==row.id&&!row.status){
        this.$notify.warning("操作不合法")
        row.status=true
      }
      //修改其他用户状态
      request.put("/admin/",row).then(res=>{
        if(res.code=='200'){
          this.$notify.success("修改成功")
          this.initAdmin()
        }else{
          this.$notify.error("修改失败")
        }
      })

    },
    saveChange(){
      this.$refs['dialogForm'].validate((valid) => {
        if(valid){
          request.put("/admin/password",this.form).then(res=>{
            if(res.code=='200'){
              //若是修改当前登录的账号密码，要重新登录
              if(this.admin.id==this.form.id){
                Cookies.remove('admin')
                this.$router.push('/login')
                this.$notify.success("修改成功");
              }else{
                this.$notify.success("修改成功");
                this.dialogFormVisible=false;
              }
            }else{
              this.$notify.error("更新失败")
            }
          })
        }
      })
    },
    handleChangePass(row){
      this.form=row
      this.dialogFormVisible=true
    },
    initAdmin() {
      request.get("/admin/page", {
        params: this.params
      }).then(res => {
        if (res.code == '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    reset() {
      this.params = {
        pageSize: 10,
        pageNum: 1,
        username: '',
        phone: '',
        email: ''
      }
      this.initAdmin()
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum
      this.initAdmin()
    },
    del(id) {
      request.delete("/admin/" + id).then(res => {
        if (res.code == '200') {
          this.$notify.success("删除成功")
          this.initAdmin()
        }
      })
    }
  }
}
</script>

<style scoped>

</style>